<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- card title -->
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept()">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- card body -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              style="width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(data,1)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="addDept(data,2)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>
      </el-card>
    </div>
    <AddDept ref="editDept" :all-depts="allDepts" :show="show" :curr-dept="currDept" @update-list="getDepartData" @close-dialog="show = $event" />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils/index.js'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data() {
    return {
      list: [],
      allDepts: [],
      show: false,
      currDept: null,
      company: { name: '', manager: 'CEO' },
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartData()
  },
  methods: {
    async getDepartData () {
      const data = await getDepartments()
      console.table(data.data.depts)
      this.allDepts = data.data.depts
      this.company.name = data.data.companyName
      // console.log(data)
      // console.log(transformTreeData(data.data.depts))
      // console.log(data.data.companyName)
      this.list = transformTreeData(data.data.depts)
    },
    // 删除
    async delDept(val) {
      try {
        await this.$confirm(`确认要删除${val.name}吗？`, '提示')
        // console.log(66)
        if (val.children && val.children.length > 0) {
          return this.$message.error('包含子的部门不能删除，先删子')
        }
        await delDepartments(val.id)
        this.getDepartments()
        this.$message.success('删除成功！')
      } catch (error) {

        // console.log(val)
      }
    },
    addDept(currDept, type) {
      console.log(type === 1 ? '新增' : '编辑')

      this.show = true
      this.currDept = currDept
      if (type === 2) {
        // 编辑需要调用接口回显数据
        //  this.$refs.editDept === AddDept组件this
        this.$refs.editDept.getDepartDetail(currDept.id)
      }
    }
  }
}

</script>

<style lang ="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}

</style>
